package com.ks.user.controller;


import com.ks.core.base._ControllerBase;
import com.ks.core.core.Result;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.user.aop.Author;
import com.ks.user.controller.dto.EmployeeRequest;
import com.ks.user.controller.dto.LoginRequest;
import com.ks.user.entity.UserBase;
import com.ks.user.service.IUserBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kingdee123
 * @since 2020-11-18
 */
@RestController
@RequestMapping("/userBase")
@Api(value = "租户用户模块", description = "租户用户模块")
public class UserBaseController extends _ControllerBase {

    @Autowired
    private IUserBaseService userBaseService;

    @PostMapping("/login")
    @ApiOperation(value = "租户用户登录", notes = "租户用户登录")
    public Object Login(@RequestBody LoginRequest loginRequest) {
        Map<String, Object> login = userBaseService.login(loginRequest.getMobile(), loginRequest.getPassword(), loginRequest.getCode(), loginRequest.getUserName(),loginRequest.getTenantId());
        return Result.success(login);
    }


    @Author
    @PostMapping("/signOut")
    @ApiOperation(value = "租户用户退出登录", notes = "租户用户退出登录")
    public Object SignOut() {
        int i = userBaseService.SignOut(getContext());
        return Result.success(i);
    }

    @PostMapping("/queryInfo")
    public Object queryUserInfo(@RequestBody LoginRequest request) {
        Map<String, Object> userInfo = userBaseService.getUserInfo(request.getUserId());
        return Result.success(userInfo);
    }

    @PostMapping("/ResetPassword")
    @ApiOperation(value = "系统用户重置密码", notes = "系统用户重置密码")
    public Object ResetPassword(@RequestBody LoginRequest request) {
        int i = userBaseService.ResetPassword(request.getUserId(), request.getMobile(), request.getCode());
        return Result.success(i);
    }

    @PostMapping("/queryTenantUser")
    @ApiOperation(value = "租户个人信息获取接口", notes = "租户个人信息获取接口")
    public Object queryTenant(@RequestBody LoginRequest request) {
        UserBase queryTenant = userBaseService.getQueryTenant(getContext(), request.getUserId());
        return Result.success(queryTenant);
    }

    @PostMapping("/updateTenantUser")
    @ApiOperation(value = "修改租户个人信息接口", notes = "修改租户个人信息接口")
    public Object updateTenantUser(@RequestBody UserBase userBase) {
        int i = userBaseService.updateTenantUser(getContext(), userBase);
        return Result.success(i);
    }

    @PostMapping("/changeTenantPassword")
    @ApiOperation(value = "租户用户密码修改接口", notes = "租户用户密码修改接口")
    public Object changeTenantPassword(@RequestBody LoginRequest request) {
        int i = userBaseService.changeTenantPassword(getContext(), request.getMobile(), request.getCode(), request.getPassword(), request.getNewPassword());
        return Result.success(i);
    }

    @PostMapping("/getEmployeeUserList")
    @ApiOperation(value = "组织内员工用户清单获取接口", notes = "组织内员工用户清单获取接口")
    public Object getEmployeeUserList(@RequestBody SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> userList = userBaseService.getUserList(getContext(),searchCriteriaBase);
        return Result.success(userList);
    }


    @PostMapping("/resetEmployeeUserPassword")
    @ApiOperation(value = "租户的重置员工用户密码接口，创建员工用户，设置管理员接口", notes = "租户的重置员工用户密码接口，创建员工用户，设置管理员接口 ")
    public Object ResetEmployeeUserPwd(@RequestBody EmployeeRequest request) {
        int i = userBaseService.ResetEmployeeUserPwd(getContext(), request.getType(),request.getEmployeeId());
        return Result.success(i);
    }


    @PostMapping("/setUserLock")
    @ApiOperation(value = "租户的开启/关闭员工用户接口", notes = "租户的开启/关闭员工用户接口")
    public Object setUserLock(@RequestBody LoginRequest request) {
        int i = userBaseService.setUserLock(request.getUserId(), request.getLock());
        return Result.success(i);
    }


    @PostMapping("/getEmployeeInfo")
    @ApiOperation(value = "获取员工用户的基本信息", notes = "获取员工用户的基本信息")
    public Object getEmployeeInfo() {
        UserBase userBaseInfo = userBaseService.getUserBaseInfo(getContext());
        return Result.success(userBaseInfo);
    }

    @PostMapping("/updateTenantMobile")
    @ApiOperation(value = "修改租户管理员手机号", notes = "修改租户管理员手机号")
    public Object updateTenantMobile(@RequestBody LoginRequest request) {
        int i = userBaseService.updateMobile(getContext(), request.getMobile(), request.getCode(), request.getPassword());
        return Result.success(i);
    }
}

